<?php
//读取团队成绩的排名
function teamSummary($mt_id,$rd_id,$mt_tmgroup){
	global $db,$dbprefix;
	if (!empty($mt_id) && !empty($rd_id)){
		$sql = "SELECT `id`,`team_ch_name` FROM `".$dbprefix."team` WHERE mt_id = '".$mt_id."' AND `act` = '1' ";
		if (is_numeric($mt_tmgroup)) $sql .= " AND `group` = '".$mt_tmgroup."' ";
		$res = $db->SelectSQL($sql);
		$num = count($res);
		if ($num>0){
			foreach ($res as $i => $n){
				$rank_sql = "SELECT tu_tm_id, tu_sts, tu_score, tu_rank, tu_rank_code, tu_cuser FROM ".$dbprefix."team_summary WHERE tu_tm_id = '".$n['id']."' AND tu_mt_id = '".$mt_id."' AND tu_rd_id = '".$rd_id."' AND tu_type = '' ";
				if($round_id == 2){
					$rank_sql .= "AND tu_notes <> 'R1' ";
				} else if ($round_id == 3) {
					$rank_sql .= "AND tu_notes <> 'R1' AND tu_notes <> 'R2' ";
				} else if ($round_id == 4){
					$rank_sql .= "AND tu_notes <> 'R1' AND tu_notes <> 'R2' AND tu_notes <> 'R3' ";
				}
				$rank_res = $db->SelectSQL($rank_sql);
				$rank_num = count($rank_res);
				if ($rank_num>0){
					$arr[$n[0]]['rank'] = (empty($rank_res[0]['tu_cuser'])) ? $rank_res[0]['tu_rank_code'].$rank_res[0]['tu_rank'] : $rank_res[0]['tu_cuser'];
					$arr[$n[0]]['tm_name'] = $n['team_ch_name'];
				} else {
					$arr[$n[0]]['rank'] = '';
					$arr[$n[0]]['tm_name'] = $n['team_ch_name'];
				}
			}	
		}
	}
	return $arr;
}


//团队赛团队成员
function getTeamPlayer($mt_id,$rd_id){
	global $db,$dbprefix;
	if (!empty($mt_id)){
		$sql = "SELECT a.tp_tm_id, b.pl_cn_name,c.gpd_line,a.tp_pl_id,c.gpd_id FROM ".$dbprefix."team_player AS a
				LEFT JOIN `".$dbprefix."player` AS b ON a.tp_pl_id = b.pl_id
				LEFT JOIN (SELECT gpd_player,gpd_line,gpd_id FROM `".$dbprefix."group`,`".$dbprefix."group_detail` WHERE gp_id = gpd_id AND gp_mt_id = '".$mt_id."' AND gp_rd_id = '".$rd_id."') AS c ON a.tp_pl_id = c.gpd_player WHERE a.tp_notes = '".$mt_id."' ORDER BY a.tp_tm_id ASC,c.gpd_id ASC,c.gpd_line ASC ";
		$res = $db->SelectSQL($sql);
		$num = count($res);
		if ($num>0){
			$k=1;
			foreach ($res as $i => $n){
				if ($k==1){
					$one = $n[0];
				}
				if ($one==$n[0]){
					$arr['pl_name'][$n[0]][$k] = $n[1];
					$arr['pl_id'][$n[0]][$k] = $n[3];
				} else {
					$k=1;
					$one = $n[0];
					$arr['pl_name'][$n[0]][$k] = $n[1];
					$arr['pl_id'][$n[0]][$k] = $n[3];
				}
				$arr['pl_num'][$n[0]]++;
				$k++;	
			}
		}
	}
	return $arr;
}


//团队赛TOTAL总成绩
function getTeamTotal($mt_id){
	global $db,$dbprefix;
	if (!empty($mt_id)){
		$sql = "SELECT tu_tm_id,tu_rd_id,tu_score,tu_to_par FROM ".$dbprefix."team_summary WHERE tu_mt_id = '".$mt_id."' ORDER BY tu_tm_id, tu_rd_id ";
		$res = $db->SelectSQL($sql);
		$num = count($res);
		if ($num>0){
			foreach ($res as $i => $n){
				$arr['score'][$n[0]][$n[1]] = $n[2];
				$arr['to_par'][$n[0]][$n[1]] = $n[3];
				$arr['total_topar'][$n[0]] += $n[3];		
				$arr['total_score'][$n[0]] += $n[2];
			}	
		}
	}
	return $arr;
}

//用于团队赛成绩读取
function teamPlayerScore($mt_id){
	global $db,$dbprefix;
	if (!empty($mt_id)){
		$sql = "SELECT sc_rd_id,sc_pl_id,sc_ho_id,sc_score,sc_to_par,sc_sts FROM ".$dbprefix."player_score
				WHERE sc_mt_id = '".$mt_id."' AND sc_type = 'M'  ORDER BY sc_ho_id ASC ";			
		$res = $db->SelectSQL($sql);
		$num = count($res);
		if ($num>0){
			foreach ($res as $i => $n){
				$arr[$n[0]][$n['sc_pl_id']][$n['sc_ho_id']] = ($n['sc_sts']=='FQ') ? 'WD' : $n['sc_score'];
			}
		}
	}
	return $arr;
}

//团队成绩中每一队的成绩
function CompareScore($sc1,$sc2,$par){
	for ($a=1;$a<=18;$a++){
		$score=$toClass='';
		if (!empty($sc1[$a]) && !empty($sc2[$a])){
			if ($sc1[$a]>$sc2[$a]){
				$score = $sc2[$a];
			} else {
				$score = $sc1[$a];
			}
			
			$topar = $score-$par[$a-1];
		} else {
			if ($sc1[$a]=='WD'){
				$score = $sc2[$a];
			} elseif ($sc2[$a]=='WD'){
				$score = $sc1[$a];
			}
		}
		if ($topar<='-2'){
			$toClass = 'eagle';
		} else if ($topar=='-1'){
			$toClass = 'birdie';
		} else if ($topar=='0'){
			$toClass = 'par';
		} else if ($topar=='1'){
			$toClass = 'bogey';
		} else if ($topar>='2'){
			$toClass = 'db_bogey';
		} else {
			$toClass = 'other';
		}
		$arr['score'][$a] = $score;
		$arr['class'][$a] = $toClass;
	}
	return $arr;
}
?>